<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="opt">
    <p id="op"></p>

    <script>
        let opt = document.getElementById('opt')
        let op = document.getElementById('op')

        let obj = {} //当作是vue实例data
        let val = '初始值'
        Object.defineProperty(obj, 'txtVal', {
            get() {
                return val //这里也就初始化数据用一下
            },
            set(newVal) {
                opt.value = newVal
                op.innerHTML = newVal
            }
        })

        // 元素初始值
        opt.value = obj.txtVal
        op.innerHTML = obj.txtVal

        // 监听数据变化
        opt.addEventListener('input', e => {
            obj.txtVal = e.target.value
        })
    </script>

</body>

</html>